<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <style>
        @keyframes fadeOut {
            0% { opacity: 1; }
            100% { opacity: 0; }
        }
        .dynamic-font {
            writing-mode: vertical-rl;
            font-size: 40px;
            animation: fadeOut 10s ease-in-out;
        }

        .hide_1 {
            opacity: 0;
            position: absolute;
            top:100px;
            left: 150px;
            animation-delay: 3s;
        }
        .hide_2 {
            opacity: 0;
            position: absolute;
            top:180px;
            left: 220px;
            animation-delay: 6s;
        }

        html, body {
            height: 100%;

            background-image: url(./img/bk_img_register.jpg);
            background-repeat: no-repeat;
            background-position: center;
            background-size: cover;
}
    </style>
    <link rel="stylesheet" href="./css/common.css">
    <link rel="stylesheet" href="./css/login.css">
    <link rel="shortcut icon" href="./img/favicon.ico" >
</head>
<body>
    <div class="nav">
        五子棋对弈
    </div>
    
    <h1 class="dynamic-font hide_1">欢迎您加入</h1>
    <h1 class="dynamic-font hide_2">这万家灯火，予你</h1>

    <div class="login-container">
        <!-- 登录界面的对话框 -->
        <div class="login-dialog">
            <!-- 提示信息 -->
            <h3>别忘了你的密码哦</h3>
            <!-- 这个表示一行 -->
            <div class="row">
                <img src="./img/user.png" alt="加载失败" height="40" width="40"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="text" id="user_name" name="username">
            </div>
            <!-- 这是另一行 -->
            <div class="row">
                <img src="./img/password.png" alt="加载失败" height="35" width="35"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="password" id="password" name="password">
            </div>
            <!-- 提交按钮 -->
            <div class="row">
                <button id="submit" onclick="reg()">注册</button>
            </div>
        </div>
    </div> 

    <script src="js/jquery.min.js"></script>
    <script>
        //1. 给按钮添加点击事件，调用注册函数
        //2. 封装实现注册函数
        function reg() {
            //  1. 获取两个输入框空间中的数据，组织成为一个json串
                var user_msg = {
                    username: document.getElementById('user_name').value,
                    password: document.getElementById('password').value
                };

            //  2. 通过ajax向后台发送用户注册请求
            $.ajax({
                url: "/reg",
                type:"POST",
                data: JSON.stringify(user_msg),
                success: function(res) {
                    if (res.result == false){
                        //  3. 如果请求失败，则清空两个输入框内容，并提示错误原因
                        document.getElementById("user_name").value = "";
                        document.getElementById("password").value = "";
                        alert(res.reason);
                    }else
                    {
                        //  3. 如果请求成功，则跳转到登录页面
                        alert(res.reason);
                        window.location.assign("/login.html");
                    }
                },
                error: function(xhr){
                    document.getElementById("user_name").value = "";
                    document.getElementById("password").value = "";
                    alert(JSON.stringify(xhr));
                }
            });    
        }
    </script>
</body>
</html>